package com.smartbus.bus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.smartbus.common.annotation.Log;
import com.smartbus.common.core.controller.BaseController;
import com.smartbus.common.core.domain.AjaxResult;
import com.smartbus.common.enums.BusinessType;
import com.smartbus.bus.domain.BusRoutes;
import com.smartbus.bus.domain.dto.DispatchRouteDTO;
import com.smartbus.bus.service.IBusRoutesService;
import com.smartbus.common.utils.poi.ExcelUtil;
import com.smartbus.common.core.page.TableDataInfo;
import java.util.List;

/**
 * 公交线路基础信息，存储各条公交线路的基本配置和运营参数Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/bus/routes")
public class BusRoutesController extends BaseController
{
    @Autowired
    private IBusRoutesService busRoutesService;

    /**
     * 查询公交线路基础信息，存储各条公交线路的基本配置和运营参数列表
     */
    @PreAuthorize("@ss.hasPermi('bus:routes:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusRoutes busRoutes)
    {
        startPage();
        List<BusRoutes> list = busRoutesService.selectBusRoutesList(busRoutes);
        return getDataTable(list);
    }

    /**
     * 导出公交线路基础信息，存储各条公交线路的基本配置和运营参数列表
     */
    @PreAuthorize("@ss.hasPermi('bus:routes:export')")
    @Log(title = "公交线路基础信息，存储各条公交线路的基本配置和运营参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusRoutes busRoutes)
    {
        List<BusRoutes> list = busRoutesService.selectBusRoutesList(busRoutes);
        ExcelUtil<BusRoutes> util = new ExcelUtil<BusRoutes>(BusRoutes.class);
        util.exportExcel(response, list, "公交线路基础信息，存储各条公交线路的基本配置和运营参数数据");
    }

    /**
     * 获取公交线路基础信息，存储各条公交线路的基本配置和运营参数详细信息
     */
    @PreAuthorize("@ss.hasPermi('bus:routes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(busRoutesService.selectBusRoutesById(id));
    }

    /**
     * 新增公交线路基础信息，存储各条公交线路的基本配置和运营参数
     */
    @PreAuthorize("@ss.hasPermi('bus:routes:add')")
    @Log(title = "公交线路基础信息，存储各条公交线路的基本配置和运营参数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusRoutes busRoutes)
    {
        return toAjax(busRoutesService.insertBusRoutes(busRoutes));
    }

    /**
     * 修改公交线路基础信息，存储各条公交线路的基本配置和运营参数
     */
    @PreAuthorize("@ss.hasPermi('bus:routes:edit')")
    @Log(title = "公交线路基础信息，存储各条公交线路的基本配置和运营参数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusRoutes busRoutes)
    {
        return toAjax(busRoutesService.updateBusRoutes(busRoutes));
    }

    /**
     * 删除公交线路基础信息，存储各条公交线路的基本配置和运营参数
     */
    @PreAuthorize("@ss.hasPermi('bus:routes:remove')")
    @Log(title = "公交线路基础信息，存储各条公交线路的基本配置和运营参数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busRoutesService.deleteBusRoutesByIds(ids));
    }

    /**
     * 获取调度系统线路数据（包含地图展示所需的路径、颜色等信息）
     */
    @PreAuthorize("@ss.hasPermi('bus:routes:list')")
    @GetMapping("/dispatch")
    public AjaxResult getDispatchRoutes()
    {
        try {
            List<DispatchRouteDTO> dispatchRoutes = busRoutesService.getDispatchRoutes();
            return AjaxResult.success("获取调度线路数据成功", dispatchRoutes);
        } catch (Exception e) {
            logger.error("获取调度线路数据失败", e);
            return AjaxResult.error("获取调度线路数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取线路实时运营状态
     */
    @PreAuthorize("@ss.hasPermi('bus:routes:list')")
    @GetMapping("/status")
    public AjaxResult getRouteStatus()
    {
        try {
            List<DispatchRouteDTO> routeStatus = busRoutesService.getRouteStatus();
            return AjaxResult.success("获取线路状态成功", routeStatus);
        } catch (Exception e) {
            logger.error("获取线路状态失败", e);
            return AjaxResult.error("获取线路状态失败: " + e.getMessage());
        }
    }
}
